
/**
 * ydcompleter class
 * @description tested in FF3,Chrome,IE8,IE7
 * @subpackage prototype
 * @author basil suter
 * @date 21-08-2009
 * @version v1.4b - added a possbility to set min keys
 */
var ydcompleter = Class.create ({
	
	initialize: function(form_id,container_div,input_id,ajax,frequenz,min_keys){
	
		this.form_id = form_id;
		this.container_div = container_div;
		this.input_id = input_id;
		this.ajax = ajax;
		this.frequenz = frequenz || 1;
		this.query = null;
		this.key_disabler = new Array(0,9,13,16,17,18,19,20,27,33,34,35,36,37,38,39,40,45);
		this.min_keys = min_keys || 0;
		this.observe();
	},
	
	observe: function(){
		
		$(this.input_id).observe('keyup', function(event){
			if(this.observer){
				clearTimeout(this.observer);
			}
			this.kd = event.keyCode;
      		this.observer = setTimeout(this.action.bind(this), this.frequenz*1000);
      		
		}.bind(this));
		
		$(this.form_id).focusFirstElement();
	},
	
	action: function(){
		
		this.query = $F(this.input_id);
		if(this.query.length >= this.min_keys)
			if(this.query == ''){
			  $(this.container_div).hide();
	    	}else{
				if(this.in_array(this.kd,this.key_disabler) != true){
					this.keydown();
				}
				$(this.container_div).observe('mouseleave', function () {
	       			$(this.container_div).hide();
			  	}.bind(this));
			}
	},
	
	in_array: function(item,arr) {
		for(p=0;p<arr.length;p++){
			if (item == arr[p]){
				return true;
			}
		}
	},
	
	keydown: function(){
		
		new Ajax.Request(Settings.Host + this.ajax,{
			method		: 'post',
			parameters	: {query: this.query},
			onLoading	: function(){
				$(this.container_div).hide();
			}.bind(this),
			onSuccess	: function(transport){
				$(this.container_div).update(transport.responseText);
				$(this.container_div).show();
			}.bind(this)
		});
	}
});

/*
<script type="text/javascript">
	var completercfg = {
		'cmp_form_id' : 'completer_form',
		'cmp_div_container' : 'completer_container',
		'completer_input' : 'completer_input',
		'cmp_frequenz_react' : '0.3',
		'cmp_min_keys' : 2
	};
</script>
 */
if(completercfg == undefined)
{
	var completercfg = {
		'cmp_form_id' : 'completer_form',
		'cmp_div_container' : 'completer_container',
		'completer_input' : 'completer_input',
		'cmp_frequenz_react' : '0.3',
		'cmp_min_keys' : 2
	};
}

document.observe('dom:loaded',function(){
	
	/**
	 * ydcompleter config
	 * @param the form id name for focusing the input field
	 * @param the name of the container div where the results will be displayed in
	 * @param the ID of the input field for getting search keys
	 * @param the name to the ajax file ex. ajax.php or to call a function use ajax.php?function=myresponse - search value: $_GET['query']
	 * @param frequenz for the reaction delay
	 */
	 cmp_form_id			= completercfg.cmp_form_id;
	 cmp_div_container 		= completercfg.cmp_div_container;
	 cmp_input_completer 	= completercfg.completer_input;
	 cmp_ajax_file_list 	= '?controller=search';
	 cmp_frequenz_react 	= completercfg.cmp_frequenz_react;
	 cmp_min_keys			= completercfg.cmp_min_keys;
	 
	if(document.getElementById(cmp_div_container) && document.getElementById(cmp_input_completer)){
		completer = new ydcompleter(cmp_form_id,cmp_div_container,cmp_input_completer,cmp_ajax_file_list,cmp_frequenz_react, cmp_min_keys);
	}
});